package cn.itsource.wechat.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.wechat.domain.BinderDto;
import cn.itsource.wechat.domain.WechatProperties;
import cn.itsource.wechat.service.IWeChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 不能用RestController ,会帮你转成json
 */
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WeChatController {

    @Autowired
    private IWeChatService weChatService;

    @Autowired
    private WechatProperties wechatProperties;

    @GetMapping("/test")
    public void test() {
        //WechartProperties(tokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code, authorizationCodeUrl=https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect, userinfoUrl=https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s, refreshTokenUrl=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s, appId=wxd853562a0548a7d0, appSecrect=4a5d5615f93f24bdba2ba8534642dbb6, redirectUri=http://bugtracker.itsource.cn/wechat/callback)
        System.out.println(wechatProperties);

    }

    /**
     * 重定向到一个地址
     * return "redirect:路径"
     *
     * @return
     */
    @GetMapping("/tologin")
    public String tologin() {
       return weChatService.tologin();
    }

    /**
     * 回调
     * 2.设置回调获取code
     */
    @GetMapping("/callback")
    public String callback(String code) {

        log.info("code ,,,,,,,{}", code);
        //回调的业务
        return weChatService.callback(code);
    }
    /**
     * 回调
     * 2.设置回调获取code
     */
    @PostMapping("/binder")
    @ResponseBody
    public AjaxResult binder(@RequestBody BinderDto dto){

        log.info("dto ,,,,,,,{}", dto);
        //绑定业务
        return weChatService.binder(dto);
    }

}
